var results = [], service, iw;
 
function Intitial_SearchGIS() {
	 var link = "http://172.16.195.14/ArcGIS/rest/services/mwaservice/MapServer";
	 service = new gmaps.ags.MapService(link);
		google.maps.event.addListenerOnce(service, 'load', function() {
		  // document.getElementById('txt_search').disabled = false;
		});
		google.maps.event.addListener(gmaps.ags.Util, 'jsonpstart', function() {
		 // document.getElementById('busy').style.visibility = 'visible';
		});
		google.maps.event.addListener(gmaps.ags.Util, 'jsonpend', function() {
		 // document.getElementById('busy').style.visibility = 'hidden';
		});
		iw = new google.maps.InfoWindow();
}
// find results ....
function find(q) {		
   closePopup();	
  removeOverlays();
  if (iw) 
    iw.close();
  document.getElementById('results').innerHTML = '';
  var exact = document.getElementById('exact').checked;
// var ArrayDeflaut=["CITY_NAME", "NAME", "SYSTEM", "STATE_ABBR", "STATE_NAME"];
  var ArrayNew=["ROAD_NAME", "LOCATION", "NAME" ];
  var params = {
    returnGeometry: true,
    searchText: q,
    contains: !exact,
    layerIds: [42,67], // city, state
    searchFields: ArrayNew,
    sr: 4326
  };
  service.find(params, processFindResults);
}

function processFindResults(rs) {
	console.log("rs", rs);
  var fs = rs.results;
      
  for (var i = 0;i < fs.length; i++) {
    processFindResult(fs[i]);  
  }
  
 // $( "div#popup_search" ).popup("open");
  $( "div#popup_search" ).panel("open");
}

function processFindResult(res) {  
 
  var feat = res.feature;
  var ovs = feat.geometry;
  var a = feat.attributes;
      
  var title = res.layerName + ':' + res.foundFieldName + ' = ' + res.value + '<br/>';
  
  var html = '<div style="width:300px;height:300px;overflow:auto;font-size:small">';
  html += '<b>' + title + '</b>';
  for (var x in a) {
    if (a.hasOwnProperty(x)) {
      // console.log(a[x]);
      html += x + ': ' + a[x] + '<br/>';
    }
  }
  html += '</div>';

  for (var j = 0, jc = ovs.length; j < jc; j++) {
    var ov = ovs[j];      
    google.maps.event.addListener(ov, 'click', function() {

      map.setZoom(15);
      map.setCenter( ov.getPosition() ? ov.getPosition() : getPolyCenter(ov)); 

      var latlng = ov.getPosition() ? ov.getPosition() : getPolyCenter(ov);          
      iw.setContent(html);
      iw.setPosition(latlng);      
      iw.open(map);

    });   
    // ov.setIcon('images/accident.png');
    results.push(ov);
    ov.setMap(map);
  }
 	 
    var side = document.getElementById('results');
    var row = '<div onclick="highlight(' + (results.length - 1) + ')" onmouseover="this.style.backgroundColor=\'#AAAAEE\'" onmouseout="this.style.backgroundColor=\'#FFFFFF\'">' + title + '</div>' ;   
    side.innerHTML += row;    
}

function highlight(i) {
  var ov = results[i]; 
  if (ov) 
    google.maps.event.trigger(ov, 'click');  
}

function getPolyCenter(poly) {
  var paths, path, latlng;
  var lat = 0;
  var lng = 0;
  var c = 0;
  paths = poly.getPaths();
  // console.log(paths);
  for (var j = 0, jc = paths.getLength(); j < jc; j++) {
    path = paths.getAt(j);
    for (var k = 0, kc = path.getLength(); k < kc; k++) {
      latlng = path.getAt(k);
      lat += latlng.lat();
      lng += latlng.lng();
      c++;      
    }
  }
  if (c > 0) {
    return new google.maps.LatLng(lat / c, lng / c);
  }
  return null;
}

function removeOverlays() {
  if (results) {
    for (var i = 0; i < results.length; i++) {
      results[i].setMap(null);
    }
    results.length = 0;
  }
}

/*
 * var start = 0; if(start == 0){ alert("d"); }
 */
// window.onload = init;
// window['find'] = find;
// window['highlight']= highlight;
